package com.pai4j.ugc.messaegqueue.consumer.redis;

import com.pai4j.common.enums.messagequeue.MessageQueueEnum;
import com.pai4j.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.dto.queue.MomentPublishMessageDTO;
import com.pai4j.ugc.messaegqueue.service.MomentPublishConsumeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(value = false)
@Slf4j
public class MomentPublishRedisMQConsumer extends AbstractRedisQueueConsumer<MomentPublishMessageDTO> {


    @Autowired
    private MomentPublishConsumeService momentPublishConsumeService;

    public void initBean() {
        new Thread(() -> MomentPublishRedisMQConsumer.this.schedule()).start();
    }

    @Override
    protected MomentPublishMessageDTO convert(String s) {

        return JsonUtil.fromJson(s, MomentPublishMessageDTO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_MOMENT_PUBLISH;
    }

    @Override
    protected void receive(MomentPublishMessageDTO data) {

        /**
         * 动态发布后置实现流程
         */
        try {
            momentPublishConsumeService.consume(data.getMoment());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void error(MomentPublishMessageDTO data, Exception ex) {
        // TODO
    }
}
